#!/usr/bin/python
#coding:utf-8
'''
爬虫获取title地址
'''

from fake_useragent import UserAgent
import requests #导入requests（网络请求），os （创建文件夹）
from bs4 import BeautifulSoup


def content(url):
    '''
    连接方法
    '''
    try:
        ua=UserAgent()
        headers={'User-Agent':ua.random} #设置反简单反爬虫机制
        res = requests.get(url,headers=headers)  #请求目标网页，爬墙网页内容，并保存
        res.encoding="utf-8"  #解决编码异常问题
        return res.text
    except BaseException:
        return ""

def getTitle(url,log):
    '''
    获取title
    '''
    text=content(url)
    if text=="":
        log.error("get Url Is Error:"+url)
        return ""
    else:
        try:
            return BeautifulSoup(text,'lxml').find("title").text
        except BaseException:
            log.error("get Url Is Error:"+url)
            return ""